package com.moon.game.command.guest.mission;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.moon.game.command.BaseCmd;
import com.moon.game.command.RequestSession;
import com.moon.game.constants.Constants;
import com.moon.game.constants.ErrorCode;
import com.moon.game.exception.GException;
import com.moon.game.manager.MissionMG;
import com.moon.game.manager.MonsterMG;
import com.moon.game.resource.model.Item;
import com.moon.game.resource.model.Mission;
import com.moon.game.service.ItemService;
import com.moon.game.service.MissionService;
import com.moon.game.vos.ErrorMsg;
import com.moon.game.vos.PlayerVo.MyMission;

/**
 * @description 查看指定任务详细信息
 * @author Ranger 作者 E-mail:haozi000005@163.com
 * @version 创建时间：2012-8-2 下午5:34:58
 */
public class ViewTheMissionCmd extends BaseCmd {

	@Override
	protected void runOnce(RequestSession req) throws GException {
		switch (req.getStatus()) {
			case STATU_INIT:
				doInit(req);
				break;
			case STATU_CHECK:
				doCheckSelf(req);
				break;
			case STATU_RETURN:
				doResponse(req);
				break;
			default:
				break;
		}
	}

	@Override
	protected void doResponse(RequestSession req) throws GException {
		String[] ps = req.getClientRequest().getPs();
		if (ps == null) {
			doResponsePsError(req);
			return;
		}
		Integer missionId = Integer.parseInt(ps[0]);
		if (missionId == null) {
			doResponsePsError(req);
			return;
		}
		
		Mission mission = MissionMG.instance.getMission(missionId);
		if (mission == null) {
			doResponseErrorMsg(req, ErrorMsg.build(ErrorCode.ERR_NO_MISSION, "没有你要查看的任务！"));
			return;
		}
		
		
		boolean canSubmit = true;
		HashMap<Integer, Integer> itemMap = new  HashMap<Integer, Integer>();
		HashMap<Integer, Integer> monsterMap = new  HashMap<Integer, Integer>();
		MissionService.analyzeMission(mission, itemMap, monsterMap);
		String playerId = req.getPlayerId();
		StringBuffer info = new StringBuffer();
		if (itemMap.size() > 0) {
			final List<Item> hadItems = ItemService.QueryItems(playerId, new ArrayList(itemMap.keySet()));
			for (Item item : hadItems) {
				info.append("收集【" + item.getName() + "】：" + item.getNum() + "/" + itemMap.get(item.getId()) + "<br />");
				if (item.getNum() < itemMap.get(item.getId())) {
					canSubmit = false;
				}
			}
		}
		
		if (monsterMap.size() > 0) {
			List<MyMission> myMissions = null;
			int type = mission.getType();
			if (type == Constants.MAIN_MISSION_TYPE) {
				//主线任务
				myMissions = req.getPlayer().getMainMissionList();
			}else if (type == Constants.DAILY_MISSION_TYPE) {
				//日常任务
				myMissions = req.getPlayer().getDailyMissionList();
			}
			if (myMissions != null) {
				for (MyMission myMission : myMissions) {
					if (missionId == myMission.getMissionId()) {
						HashMap<Integer, Integer> process = myMission.getMonsterProcess();
						if (process != null) {
							for (Map.Entry<Integer, Integer> entry : monsterMap.entrySet()) {
								int monsterId = entry.getKey();
								if (process.containsKey(monsterId)) {
									info.append("捕获【" + MonsterMG.instance.getMonsterById(monsterId).getName() + "】：" + process.get(monsterId) + "/" + entry.getValue() + "<br />");
									if (process.get(monsterId) < entry.getValue()) {
										canSubmit = false;
									}
								}
							}
						}else {
							for (Map.Entry<Integer, Integer> entry : monsterMap.entrySet()) {
								int monsterId = entry.getKey();
								info.append("捕获【" + MonsterMG.instance.getMonsterById(monsterId).getName() + "】：" + 0 + "/" + entry.getValue() + "<br />");
							}
							canSubmit = false;
						}
					}
				}
			}
		}
		
		req.setVO("mission_id", missionId);
		req.setVO(TAG_INFO, info.toString());
		req.setVO("can_submit", canSubmit);
		response(req, req.getClientResponse());
	}

}
